package com.mlethe.android.demo.recyclerview.adapter.adapter;

import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.mlethe.android.demo.recyclerview.adapter.entity.CatDaoEntity;
import com.mlethe.demo.recyclerview.adapter.R;
import com.mlethe.library.recyclerview.adapter.BaseQuickAdapter;
import com.mlethe.library.recyclerview.listener.OnItemClickListener;
import com.mlethe.library.recyclerview.viewholder.ViewHolder;

import java.util.List;

/**
 * @author Mlethe
 * @date 2021/7/30
 */
public class FieldGuildGridAdapter extends BaseQuickAdapter<CatDaoEntity> {

    public int index = 30;

    public FieldGuildGridAdapter() {
        super(R.layout.item_field_guild);
    }

    @Override
    protected void convert(@NonNull ViewHolder holder, CatDaoEntity item, int position) {
        if (item == null) {
            holder.setVisibility(View.INVISIBLE);
            return;
        }
        ImageView imageView = holder.setVisibility(View.VISIBLE)
                .setText(R.id.name, item.name)
                .setOnItemClickListener((OnItemClickListener<CatDaoEntity>) (view, item1, position1) -> {
                    if (position1 == index) return;
                    notifyItemChange(index, "unchecked");
                    notifyItemChange(position1, "checked");
                    index = position1;
                })
                .getView(R.id.avatar);
        if (position == index) {
            holder.setBackgroundResource(R.id.avatar, R.mipmap.ic_field_guild_selected_bg);
        } else {
            holder.setBackgroundResource(R.id.avatar, 0);
        }
    }

    @Override
    protected void convert(@NonNull ViewHolder holder, CatDaoEntity item, int position, @NonNull List<Object> payloads) {
        if (payloads.contains("unchecked")) {
            holder.setBackgroundResource(R.id.avatar, 0);
        } else if (payloads.contains("checked")) {
            holder.setBackgroundResource(R.id.avatar, R.mipmap.ic_field_guild_selected_bg);
        }
    }
}
